<?php
class profile extends controller{
	public $profile;
	public $id;

	public function __construct($url){
		parent::__construct($url, true);
		$this->profile = null;
		$this->id = null;
	}

	public function check(){}

	public function run(){
		$this->getProfile();
		$this->profile();
	}

	private function getProfile(){
		$sql = "SELECT name, email FROM user WHERE id=".$_SESSION["user"][0];
		$tmp = $this->database->fetchRow($sql);
		$this->profile = $tmp[0];
	}

	private function profile(){
		if(isset($_POST["profile"])){
			if(!empty($_POST["name"]) && !empty($_POST["email"])){
				$sql = "UPDATE user SET name='".$_POST["name"]."', email='".$_POST["email"]."'";
				if(!empty($_POST["password"]) && $_POST["password"] == $_POST["password2"]) $sql .= ", password='".md5($_POST["password"])."'";
				$sql .= " WHERE id=".$_SESSION["user"][0];
				if($this->database->query($sql)){
					$this->redirect($this->self, "A mentés sikeres volt!");
				}
				else{
					$this->message = "A mentés sikertelen volt!";
				}
			}
			else{
				$this->message = "Üresen hagyott mező!";
			}
		}
	}
}
?>
